//
//  DistanceSelectorView.m
//  JiuLe
//
//  Created by 黄鑫 on 16/9/23.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "DistanceSelectorView.h"

@interface DistanceSelectorView()<UIScrollViewDelegate>{
    int cardNumber;
    float startContentOffsetX;
    float willEndContentOffsetX;
    float endContentOffsetX;
}
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) NSMutableArray *arrLabNumber;
@end

@implementation DistanceSelectorView

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self) {
        self.backgroundColor = [UIColor clearColor];
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetHeight(self.frame), CGRectGetHeight(self.frame))];
        [self initView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
        [self initView];
    }
    return self;
}

- (void)initView{
    UIImageView *imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetHeight(self.frame), CGRectGetHeight(self.frame))];
    imageBg.image = [UIImage imageNamed:@"icon_circle"];
    [self addSubview:imageBg];
    imageBg.center = self.center;
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
    [self addSubview:self.scrollView];
    self.scrollView.center = self.center;

}

- (void)initCard:(ShowType)showType{
    cardNumber = 15;
    _showType = showType;
    for (int i = 0; i < cardNumber; i++) {
        UILabel *labNumber = [[UILabel alloc] init];
        labNumber.frame = CGRectMake(CGRectGetHeight(self.frame)*i, 0, CGRectGetHeight(self.frame), CGRectGetHeight(self.frame));
        labNumber.textAlignment = NSTextAlignmentCenter;
        labNumber.textColor = [UIColor whiteColor];
        labNumber.font = [UIFont boldSystemFontOfSize:22];
        if (i == 0 || i == 1 || i == (cardNumber - 1) || i == (cardNumber - 2)) {
            labNumber.text = @"";
        }else{
            if (_showType == ShowTypeDistance) {
                labNumber.text = [NSString stringWithFormat:@"%.1f",(i-1)*0.5];
            }else if(_showType == ShowTypeTime){
                labNumber.text = [NSString stringWithFormat:@"%d",(i-1)*5];
            }else{
                labNumber.text = [NSString stringWithFormat:@"%d",(i-1)*100];
            }
        }
        labNumber.textColor = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1.0];
         self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(labNumber.frame), CGRectGetMaxY(labNumber.frame));
        [self.scrollView addSubview:labNumber];
        [self.arrLabNumber addObject:labNumber];
    }
    self.scrollView.contentOffset = CGPointMake(2*CGRectGetHeight(self.frame),0);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int current = scrollView.contentOffset.x;
    int labW = CGRectGetHeight(self.frame);
    int i = 2;
    if (startContentOffsetX > endContentOffsetX ) {
        i = floor(current/labW);
    }else if ( startContentOffsetX < endContentOffsetX) {
        i = ceilf(current/labW);
    }
    for (int j = 0; j < 5; j++) {
        UILabel *label = _arrLabNumber[i + j];
        if (j == 0 || j == 4) {
            label.textColor = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1.0];
            label.font = [UIFont systemFontOfSize:16];
            label.layer.zPosition = -5;
        }else if (j == 1 || j == 3){
            label.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
            label.font = [UIFont boldSystemFontOfSize:18];
            label.layer.zPosition = 0;
        }else{
            label.textColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
            label.font = [UIFont boldSystemFontOfSize:22];
            label.layer.zPosition = 10;
        }
    }
    if (_showType == ShowTypeDistance) {
        [self.deleaget backSelect:(i*0.5)+0.5];
    }else if(_showType == ShowTypeTime){
        [self.deleaget backSelect:(i*5)+5];
    }else{
        [self.deleaget backSelect:(i*100)+100];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    startContentOffsetX = scrollView.contentOffset.x;
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    willEndContentOffsetX = scrollView.contentOffset.x;
    scrollView.contentOffset = CGPointMake(round(willEndContentOffsetX/CGRectGetHeight(self.frame))*CGRectGetHeight(self.frame),0);
    NSLog(@"呵呵呵\n ");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    endContentOffsetX = scrollView.contentOffset.x;
    scrollView.contentOffset = CGPointMake(round(endContentOffsetX/CGRectGetHeight(self.frame))*CGRectGetHeight(self.frame),0);
    NSLog(@"哈哈哈\n ");
    [self setScrollLabel:scrollView.contentOffset.x];
}


- (void)setScrollLabel:(int)contentOffsetX{
    int labW = CGRectGetHeight(self.frame);
    int i = 2;
    if (startContentOffsetX > endContentOffsetX ) {
        i = floor(contentOffsetX/labW);
    }else if ( startContentOffsetX < endContentOffsetX) {
        i = ceilf(contentOffsetX/labW);
    }
    for (int j = 0; j < 5; j++) {
        UILabel *label = _arrLabNumber[i + j];
        if (j == 0 || j == 4) {
            label.textColor = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1.0];
            label.font = [UIFont systemFontOfSize:16];
            label.layer.zPosition = -5;
        }else if (j == 1 || j == 3){
            label.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
            label.font = [UIFont boldSystemFontOfSize:18];
            label.layer.zPosition = 0;
        }else{
            label.textColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
            label.font = [UIFont boldSystemFontOfSize:22];
            label.layer.zPosition = 10;
        }
    }
}

#pragma mark - set/get
- (NSMutableArray *)arrLabNumber{
    if (_arrLabNumber == nil) {
        _arrLabNumber = [NSMutableArray array];
    }
    return _arrLabNumber;
}


@end
